package cn.iocoder.yudao.module.brokerage.user.controller.admin;

import cn.iocoder.yudao.framework.business.basic.controller.PageController;
import cn.iocoder.yudao.framework.business.basic.controller.SimpleDetailController;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.brokerage.user.pojo.vo.*;
import cn.iocoder.yudao.module.brokerage.user.service.AdminBrokerageUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 推广用户")
@RestController
@RequestMapping("/brokerage/user")
@Validated
public class BrokerageUserController implements SimpleDetailController<AdminBrokerageUserVO>, PageController<AdminBrokerageUserVO, AdminBrokerageUserQueryDTO> {
    @Resource
    private AdminBrokerageUserService adminBrokerageUserService;

    @Resource
    private MemberUserApi memberUserApi;

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    @Override
    public CommonResult<AdminBrokerageUserVO> getDetail(Long id) {
        return success(adminBrokerageUserService.getDetail(id));
    }

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    @Override
    public CommonResult<PageResult<AdminBrokerageUserVO>> getPage(Integer pageNo, Integer pageSize, AdminBrokerageUserQueryDTO param) {
        return success(adminBrokerageUserService.getPage(pageNo, pageSize, param));
    }

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    @PostMapping("/bind")
    @Operation(summary = "绑定推广关系")
    public CommonResult<Boolean> bindRelation(@Valid @RequestBody AdminBrokerageUserBindVO bindVO) {
        return success(memberUserApi.bindRelation(bindVO.getUserId(), bindVO.getFatherUserId()));
    }

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    @PostMapping("/clear")
    @Operation(summary = "绑定推广关系")
    public CommonResult<Boolean> clearFatherAndGrandpa(@Valid @RequestBody AdminBrokerageUserClearVO clearVO) {
        return success(adminBrokerageUserService.clearFatherAndGrandpa(clearVO.getId()));
    }

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    @PostMapping("/enabled")
    @Operation(summary = "分佣资格开关")
    public CommonResult<Boolean> updateEnabled(@Valid @RequestBody AdminBrokerageUserEnabledDTO enabledVO) {
        return success(adminBrokerageUserService.updateEnabled(enabledVO.getId(), enabledVO.getEnabled()));
    }

}